"""
author: liuxu
date: 2025/5/29
description: 复测bug场景用例
"""
import os
import allure
import pytest

from common.logger_ctrl import mylogger
from common.ssh_command import exec_cmd
from base_public.testbase import BasePublic
from setting import CPP_DIR


@pytest.mark.medium
@allure.feature('sqtt相关bug')
class TestSqttBugCase(BasePublic):
    def test_bug_59894(self, test_env, dcu_info):
        """
        Bug链接: http://hpczentao.sugon.com/bug-view-59894.html
        标题: prof包含s_call_b64指令汇编代码报错
        问题描述: sqtt问题
        :return:
        """
        # 编译用例
        gfx_arch = dcu_info["gfx_arch"]
        src_case_dir = os.path.join(CPP_DIR, "bug_case/bug-59894")
        # ret0, output0 = exec_cmd(f"make GFX_ARCH={gfx_arch} -C {src_case_dir}", cwd=test_env)  # 使用Makefile编译
        compile_cmds = f'''clang -x assembler -target amdgcn-amd-amdhsa -mcode-object-version=4 -mcpu={gfx_arch} -c {src_case_dir}/kernel_test.s -o kernel_test.o
        clang -target amdgcn-amd-amdhsa {test_env}/kernel_test.o -o kernel_test.co
        hipcc {src_case_dir}/demo.cpp -o test'''
        for cmd in compile_cmds.strip().split("\n"):
            ret0, output0 = exec_cmd(cmd.strip(), cwd=test_env)
            assert ret0 is True and "error:" not in output0
        assert self.exist_file("test"), "compile error: not found test"
        # 执行sqtt测试用例
        ret, output = exec_cmd(f"hipprof --sqtt ./test 16 64 128 128 27 3 3 1 1 1 1 1", cwd=test_env)
        assert ret is True and self.check_sqtt_is_success(output, cwd=test_env) is True

    # @pytest.skipif(906,926)
    def test_bug_67858(self, test_env, dcu_info):
        """
        Bug链接: http://hpczentao.sugon.com/bug-view-67858.html
        标题: hipprof包含 v_mmac 汇编代码报错且出现未遇到过的指令类型
        问题描述: sqtt问题
        :return:
        """
        # 判断环境，用例只在kme、bw测试
        gfx_arch = dcu_info["gfx_arch"]
        if gfx_arch in ["gfx906", "gfx926"]:
            pytest.skip("instruction not supported on this GPU")
        # 编译用例
        des_exe_name = self.compile_source_code(
            src_file_name="bug_case/bug-67858/mmop.cpp",
            compile_paras=f"--offload-arch={gfx_arch}")

        # 执行sqtt测试用例
        ret, output = exec_cmd(f"hipprof --sqtt {des_exe_name} 1 256 64 64 256 3 3 0 0 1 1 1 1", cwd=test_env)
        assert ret is True and self.check_sqtt_is_success(output, cwd=test_env) is True

    def test_bug_73001(self, test_env, dcu_info):
        """
        Bug链接: http://hpczentao.sugon.com/bug-view-73001.html
        标题: sqtt调试demo失败
        问题描述: sqtt问题
        解决方案：增加参数--flush-mode，当该参数设置不为 「1」时，不添加GPU_FLUSH_ON_EXECUTION环境变量，并给出可能抓取到错误数据的警告
        :return:
        """
        # 判断环境，用例只在kme、bw测试
        dcu_type = dcu_info["dcu_type"]
        if dcu_type == "Vega 20":  # 昆山ac会报错Encountered HIP error (2) at line 150 in file ···
            pytest.skip("Testcase ll_latency_test not supported on this GPU")

        # 编译用例
        des_exe_name = self.compile_source_code(src_file_name="bug_case/bug-73001/ll_latency_test.cpp", compile_paras=f"-O3 -g")

        cfg_name = os.path.join(self.src_path, "bug_case/bug-73001/filter.txt")

        # 执行sqtt测试用例
        ret, output = exec_cmd(f"hipprof --sqtt -i {cfg_name} --flush-mode 0 ROC_GLOBAL_CU_MASK=0x1 {des_exe_name} 0 1", cwd=test_env)
        assert ret is True
        assert self.check_sqtt_is_success(output, check_gen_files=False, cwd=test_env) is True

    def test_bug_93638(self, test_env):
        """
        Bug链接: http://hpczentao.sugon.com/bug-view-93638.html
        标题: sqtt 解析汇编代码出现匹配错误
        问题描述: sqtt问题（正则表达式编写错误导致汇编解析错误，HIP_PROF:assembly check fail! JUMP）
        :return:
        """
        # 编译用例
        des_exe_name = self.compile_source_code(src_file_name="bug_case/bug-93638/bug93638-multi_gpu_communication.cpp")
        # 执行sqtt测试用例
        ret, output = exec_cmd(f"hipprof --sqtt --sqtt-type all -d ./ -o ./ {des_exe_name}", cwd=test_env)
        assert ret is True and self.check_sqtt_is_success(output, cwd=test_env) is True




